import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class searchuser
 */
@WebServlet("/searchuser")
public class searchuser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public searchuser() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession sesja = request.getSession(true);
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		if(sesja.getAttribute("log")!=null && sesja.getAttribute("log").equals("pracownik")){
			out.write("<script type='text/javascript' src='js/searchuser.js'></script>");
			if(sesja.getAttribute("lang").equals("en")){
				out.write("Enter the user surname:");
				out.write("<form id=uzytkownicy name=uzytkownicy><input type='text' id='nazwisko' size='20' name='nazwisko'/><input type='button' id='but1' name='but1' value='Search' onClick='szukaj()'>" +
						"<input type='button' id='ok' name='ok' value='Clean' onClick='czysc()'></form>");
				out.write("<div id=wynik></div>");
			}else{
				out.write("Wprowadź nazwisko uzytkownika:");
				out.write("<form id=uzytkownicy name=uzytkownicy><input type='text' id='nazwisko' size='20' name='nazwisko'/><input type='button' id='but1' name='but1' value='Szukaj' onClick='szukaj()'>" +
						"<input type='button' id='ok' name='ok' value='Wyczyść' onClick='czysc()'></form>");
				out.write("<div id=wynik></div>");
			}
		}
		else{
			if(sesja.getAttribute("lang").equals("en")){
				out.write("You don't have permission to acces this page. Return to <a href=index.jsp>main page</a>");
			}else{
				out.write("Nie masz uprawnień do oglądania tej strony. Wróć na stronę główną: <a href=index.jsp>Strona główna</a>");
			}
			
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession sesja = request.getSession(true);
		ArrayList klienci = new ArrayList();
		if(sesja.getAttribute("log")!=null && sesja.getAttribute("log").equals("pracownik")){
			try {
					if(request.getParameter("nazwisko")!=null){
						Connection con = mb.commons.DbConnection.getConnection();
						Statement stmt = con.createStatement();		
						ResultSet rs = stmt.executeQuery("SELECT id_klienta,imie,nazwisko,adres,miasto,kod FROM klienci WHERE nazwisko like '%"+request.getParameter("nazwisko").toString()+"%'");
						while(rs.next()){
							klienci.add(rs.getInt("id_klienta"));
							klienci.add(rs.getString("imie"));
							klienci.add(rs.getString("nazwisko"));
							klienci.add(rs.getString("adres"));
							klienci.add(rs.getString("miasto"));
							klienci.add(rs.getString("kod"));
						}
						if(klienci.size()!=0){
							if(sesja.getAttribute("lang").equals("en")){
								out.write("<table id='tabelawynikow' style='border: 1px solid;text-align:center'>");
								out.write("<tr bgcolor='#AAAAAA'><td></td><td style='border: 1px solid;' width=20px;>ID</td><td style='border: 1px solid;' width=100px;>Name</td><td style='border: 1px solid;' width=100px;>Surname</td><td style='border: 1px solid;' width=100px;>Address</td><td style='border: 1px solid;' width=100px;>City</td><td style='border: 1px solid;' width=75px;>P.Code</td></tr>");							
							}else{
								out.write("<table id='tabelawynikow' style='border: 1px solid;text-align:center'>");
								out.write("<tr bgcolor='#AAAAAA'><td></td><td style='border: 1px solid;' width=20px;>ID</td><td style='border: 1px solid;' width=100px;>Imie</td><td style='border: 1px solid;' width=100px;>Nazwisko</td><td style='border: 1px solid;' width=100px;>Adres</td><td style='border: 1px solid;' width=100px;>Miasto</td><td style='border: 1px solid;' width=75px;>Kod</td></tr>");							
							}
							for(int i=0;i<klienci.size();i++){
								if(sesja.getAttribute("lang").equals("en")){
									out.write("<tr><td style='border: 1px solid;'><input type='button' id='"+i+"' name='ok' value='Select' onClick='powrot(this.id)'></td>");
								}else{
									out.write("<tr><td style='border: 1px solid;'><input type='button' id='"+i+"' name='ok' value='Wybierz' onClick='powrot(this.id)'></td>");
								}
								out.write("<td style='border: 1px solid;'><input type='text' id='id"+i+"' value='"+klienci.get(i)+"' hidden/>"+klienci.get(i)+"</td>"); i++;
								out.write("<td style='border: 1px solid;'><input type='text' id='imie"+i+"' value='"+klienci.get(i)+"' hidden/>"+klienci.get(i)+"</td>"); i++;
								out.write("<td style='border: 1px solid;'><input type='text' id='nazwisko"+i+"' value='"+klienci.get(i)+"' hidden/>"+klienci.get(i)+"</td>"); i++;
								out.write("<td style='border: 1px solid;'><input type='text' id='adres"+i+"' value='"+klienci.get(i)+"' hidden/>"+klienci.get(i)+"</td>"); i++;
								out.write("<td style='border: 1px solid;'><input type='text' id='miasto"+i+"' value='"+klienci.get(i)+"' hidden/>"+klienci.get(i)+"</td>"); i++;
								out.write("<td style='border: 1px solid;'><input type='text' id='kod"+i+"' value='"+klienci.get(i)+"' hidden/>"+klienci.get(i)+"</td></tr>"); 
							}
							out.write("</table");
						}else{
							if(sesja.getAttribute("lang").equals("en")){
								out.write("No results");
							}else{
								out.write("Brak wyników");
							}								
						}
					}else{
						Connection con = mb.commons.DbConnection.getConnection();
						Statement stmt = con.createStatement();		
						ResultSet rs = stmt.executeQuery("SELECT id_klienta,imie,nazwisko,adres,miasto,kod FROM klienci");
						while(rs.next()){
							klienci.add(rs.getInt("id_klienta"));
							klienci.add(rs.getString("imie"));
							klienci.add(rs.getString("nazwisko"));
							klienci.add(rs.getString("adres"));
							klienci.add(rs.getString("miasto"));
							klienci.add(rs.getString("kod"));
						}
						if(klienci.size()!=0){
							if(sesja.getAttribute("lang").equals("en")){
								out.write("<table id='tabelawynikow' style='border: 1px solid;text-align:center'>");
								out.write("<tr bgcolor='#AAAAAA'><td></td><td style='border: 1px solid;' width=20px;>ID</td><td style='border: 1px solid;' width=100px;>Name</td><td style='border: 1px solid;' width=100px;>Surname</td><td style='border: 1px solid;' width=100px;>Address</td><td style='border: 1px solid;' width=100px;>City</td><td style='border: 1px solid;' width=75px;>P.Code</td></tr>");							
							}else{
								out.write("<table id='tabelawynikow' style='border: 1px solid;text-align:center'>");
								out.write("<tr bgcolor='#AAAAAA'><td></td><td style='border: 1px solid;' width=20px;>ID</td><td style='border: 1px solid;' width=100px;>Imie</td><td style='border: 1px solid;' width=100px;>Nazwisko</td><td style='border: 1px solid;' width=100px;>Adres</td><td style='border: 1px solid;' width=100px;>Miasto</td><td style='border: 1px solid;' width=75px;>Kod</td></tr>");							
							}
							for(int i=0;i<klienci.size();i++){
								if(sesja.getAttribute("lang").equals("en")){
									out.write("<tr><td style='border: 1px solid;'><input type='button' id='"+i+"' name='ok' value='Select' onClick='powrot(this.id)'></td>");
								}else{
									out.write("<tr><td style='border: 1px solid;'><input type='button' id='"+i+"' name='ok' value='Wybierz' onClick='powrot(this.id)'></td>");
								}
								out.write("<td style='border: 1px solid;'><input type='text' id='id"+i+"' value='"+klienci.get(i)+"' hidden/>"+klienci.get(i)+"</td>"); i++;
								out.write("<td style='border: 1px solid;'><input type='text' id='imie"+i+"' value='"+klienci.get(i)+"' hidden/>"+klienci.get(i)+"</td>"); i++;
								out.write("<td style='border: 1px solid;'><input type='text' id='nazwisko"+i+"' value='"+klienci.get(i)+"' hidden/>"+klienci.get(i)+"</td>"); i++;
								out.write("<td style='border: 1px solid;'><input type='text' id='adres"+i+"' value='"+klienci.get(i)+"' hidden/>"+klienci.get(i)+"</td>"); i++;
								out.write("<td style='border: 1px solid;'><input type='text' id='miasto"+i+"' value='"+klienci.get(i)+"' hidden/>"+klienci.get(i)+"</td>"); i++;
								out.write("<td style='border: 1px solid;'><input type='text' id='kod"+i+"' value='"+klienci.get(i)+"' hidden/>"+klienci.get(i)+"</td></tr>"); 
							}
							out.write("</table");						
						}
					}					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}		
	}

}
